package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerServiceImpl;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"customer:potentialList", "潜在客户列表"}, logical = Logical.OR)
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //潜在客户列表 只能显示潜在客户
        qo.setStatus(0);

        //根据角色编码 查询拥有该角色的员工 Market_Manager Market 市场经理和市场专员
        List<Employee> sellers = employeeService.selectEmployeeByRoleSn("Market_Manager", "Market");
        model.addAttribute("sellers", sellers);

        //根据 字典目录sn job 查询出对应的字典明细
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectDicItemTitleByDicSn("job");
        model.addAttribute("jobs", jobs);
        //根据 字典目录sn source 查询出对应的字典明细
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectDicItemTitleByDicSn("source");
        model.addAttribute("sources", sources);

        //根据 字典目录sn source 查询出对应的字典明细
        List<SystemDictionaryItem> communicationMethods = systemDictionaryItemService.selectDicItemTitleByDicSn("communicationMethod");
        model.addAttribute("communicationMethods", communicationMethods);

        PageInfo<Customer> pageInfo = customerServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/potentialList";
    }

    @RequestMapping("/list")
    @RequiresPermissions(value = {"customer:list", "客户列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //查询当前客户的状态
        List<Customer> status = customerServiceImpl.selectForStatus();
        model.addAttribute("status", status);

        //根据角色编码 查询拥有该角色的员工 Market_Manager Market 市场经理和市场专员
        List<Employee> sellers = employeeService.selectEmployeeByRoleSn("Market_Manager", "Market");
        model.addAttribute("sellers", sellers);

        //根据 字典目录sn job 查询出对应的字典明细
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectDicItemTitleByDicSn("job");
        model.addAttribute("jobs", jobs);
        //根据 字典目录sn source 查询出对应的字典明细
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectDicItemTitleByDicSn("source");
        model.addAttribute("sources", sources);

        PageInfo<Customer> pageInfo = customerServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/list";
    }

    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //资源池客户列表 只能显示资源池客户  poolList
        qo.setStatus(2);

        //根据角色编码 查询拥有该角色的员工 Market_Manager Market 市场经理和市场专员
        List<Employee> sellers = employeeService.selectEmployeeByRoleSn("Market_Manager", "Market");
        model.addAttribute("sellers", sellers);

        PageInfo<Customer> pageInfo = customerServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/poolList";
    }

    @RequestMapping("/failList")
    public String failList(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //失败客户列表 只能显示失败的客户
        qo.setStatus(3);

        //根据角色编码 查询拥有该角色的员工 Market_Manager Market 市场经理和市场专员
        List<Employee> sellers = employeeService.selectEmployeeByRoleSn("Market_Manager", "Market");
        model.addAttribute("sellers", sellers);

        PageInfo<Customer> pageInfo = customerServiceImpl.query(qo);
        model.addAttribute("result", pageInfo);
        return "customer/failList";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        customerServiceImpl.delete(id);
        return new JsonResult("删除成功", true);

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer, Long id) {
        if (id != null) {
            customerServiceImpl.update(customer);
        } else {
            customerServiceImpl.save(customer);
        }
        return new JsonResult("新增/编辑成功", true);
    }

    /**
     * 远程验证 查询手机号码是否存在
     *
     * @param tel
     * @param id
     * @return
     */
    @RequestMapping("/checkTel")
    @ResponseBody
    public Map<String, Boolean> checkTel(String tel, Long id) {
        HashMap<String, Boolean> map = new HashMap<>();
        Customer customer;
        //根据id是否为null 区分新增还是编辑
        if (id == null) {
            //新增时 根据用户提供的电话号码到数据库查询是否存在
            customer = customerServiceImpl.checkTel(tel);
            map.put("valid", customer == null);
        } else {
            //编辑客户时  先根据id获取该客户 之前的手机号码
            customer = customerServiceImpl.get(id);
            String oldTel = customer.getTel();
            //判断当前更新的号码 与之前的手机号码是否一致
            if (tel.equals(oldTel)) {
                //若与旧号码相同 则成功 返回true
                map.put("valid", true);
            } else {
                //若与旧号码不相同  拿新的电话号码到数据库查询是否存在
                customer = customerServiceImpl.checkTel(tel);
                map.put("valid", customer == null);
            }
        }
        return map;
    }

    //修改客户状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Customer customer) {
        customerServiceImpl.changeStatus(customer);
        return new JsonResult("修改客户状态成功", true);
    }
}
